<template>
  <div style="width: 400px;text-align:center;margin: 5px auto">
    <h1 STYLE="margin-bottom: 10px;">控件属性新增</h1>
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="属性类型">
        <el-select v-model="ruleForm.propertyType" placeholder="请选择">
          <el-option
            v-for="item in propertyType"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="属性标题">
        <el-input
          type="text"
          v-model="ruleForm.propertyTitle"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="宽度" >
        <el-input type="text" v-model="ruleForm.width"   ></el-input>
      </el-form-item>
      <el-form-item label="是否必填">
        <el-switch
          v-model="ruleForm.isRequired"
          active-color="#13ce66"
          inactive-color="#ff4949"
          active-value="1"
          inactive-value="0"
        >
        </el-switch>
      </el-form-item>
      <el-form-item label="选择内容">
        <el-input
          type="textarea"
          v-model="ruleForm.options"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="绑定数据模型">
        <el-input
          type="text"
          v-model="ruleForm.bindModel"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="占位符">
        <el-input
          type="text"
          v-model="ruleForm.placeholder"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="最大长度">
        <el-input
          type="text"
          v-model="ruleForm.maxLength"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="cs风格">
        <el-input
          type="text"
          v-model="ruleForm.style"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="初始化值">
        <el-input
          type="text"
          v-model="ruleForm.initVal"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="最大值">
        <el-input
          type="text"
          v-model="ruleForm.maxVal"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="最小值">
        <el-input
          type="text"
          v-model="ruleForm.minVal"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="小数位">
        <el-input
          type="text"
          v-model="ruleForm.precision"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="步长">
        <el-input
          type="text"
          v-model="ruleForm.step"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="最大行数" >
        <el-input type="text" v-model="ruleForm.maxRows" autocomplete="off"  ></el-input>
      </el-form-item>
      <el-form-item label="最小行数" >
        <el-input type="text" v-model="ruleForm.minRows" autocomplete="off"  ></el-input>
      </el-form-item>
      <el-form-item label="初始化行数" >
        <el-input type="text" v-model="ruleForm.initRowNum" autocomplete="off"  ></el-input>
      </el-form-item>
      <el-form-item label="初始化列数" >
        <el-input type="text" v-model="ruleForm.initColumnNum" autocomplete="off" @keyup.native="autoSubmit($event)" ></el-input>
      </el-form-item>
      <el-form-item>
        <div style="text-align:center">
          <el-button type="primary" @click="submitForm('ruleForm')"
            >保存</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
          <el-button @click="returnList">返回</el-button>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import request from "../utils/request";
import {propertyType} from "../const/const";

export default {
  name: "add",
  data(){

    return{
        // propertyType: [
        //   {
        //   value: 'input',
        //   label: '单行输入框'
        // },
        //   {
        //     value: 'textarea',
        //     label: '多行输入框'
        //   },
        //   {
        //     value: 'date',
        //     label: '日期'
        //   },
        //   {
        //     value: 'time',
        //     label: '时间'
        //   },
        //   {
        //     value: 'number',
        //     label: '数字输入框'
        //   },
        //   {
        //     value: 'radio',
        //     label: '单选框'
        //   },
        //   {
        //     value: 'checkbox',
        //     label: '多选框'
        //   },
        //   {
        //     value: 'select',
        //     label: '选择器'
        //   },
        //   {
        //     value: 'switch',
        //     label: '开关'
        //   }
        // ],
      ruleForm: {
        propertyType: '',
        propertyTitle: '',
        width: '',
        isRequired: '',
        options: '',
        bindModel: '',
        placeholder: '',
        maxLength: '',
        style: '',
        initVal: '',
        maxVal: '',
        minVal: '',
        precision: '',
        step: '',
        maxRows: '',
        minRows: '',
        initRowNum: '',
        initColumnNum: ''
      },
      propertyTypeConst:null,
      rules: {
      }
    }

  },
  methods: {
    autoSubmit: function (ev) {
      if (ev.code === "Enter") {
        this.submitForm('ruleForm');
      }
    },
    submitForm(formName) {
      let _this = this;
      const param = {
        controlId: _this.ruleForm.controlId,
        propertyType: _this.ruleForm.propertyType,
        propertyTitle: _this.ruleForm.propertyTitle,
        width: _this.ruleForm.width,
        isRequired: _this.ruleForm.isRequired,
        options: _this.ruleForm.options,
        bindModel: _this.ruleForm.bindModel,
        placeholder: _this.ruleForm.placeholder,
        maxLength: _this.ruleForm.maxLength,
        style: _this.ruleForm.style,
        initVal: _this.ruleForm.initVal,
        maxVal: _this.ruleForm.maxVal,
        minVal: _this.ruleForm.minVal,
        precision: _this.ruleForm.precision,
        step: _this.ruleForm.step,
        maxRows: _this.ruleForm.maxRows,
        minRows: _this.ruleForm.minRows,
        initRowNum: _this.ruleForm.initRowNum,
        initColumnNum: _this.ruleForm.initColumnNum,
      };
      this.$refs[formName].validate(valid => {
        if (valid) {
          request({
            method: "POST",
            url: "/api/ControlProperties/insert",
            data: param
          })
            .then(res => {
              if (res.data.status == "1") {
                this.$message(res.data.message);
                _this.$router.push({path: "/controlproperties/list/"+this.ruleForm.controlId});
              } else {
                this.$alert(res.data.message, {
                  confirmButtonText: '确定',
                  callback: action => {
                  }
                });
                return false;
              }
            })
            .catch(err => {
              debugger;
              console.log(err);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    returnList(){
      this.$router.push({path: "/controlproperties/list/"+this.ruleForm.controlId});
    }
  },
  mounted() {
    this.ruleForm.controlId = this.$route.params.controlId;
    this.propertyTypeConst = propertyType;
  }
}
</script>

<style scoped></style>
